home *** CD-ROM | disk | FTP | other *** search
- /****i* SOURCE_FILE/INFO
- *
- * NAME
- * Iterator.js
- *
- * USAGE
- * Part of Netobjects JavaScript Library.
- *
- * COPYRIGHT
- * Copyright ⌐ 2000-2005 Website Pros, Inc.
- * All Rights Reserved.
- *
- * This is an unpublished work protected by Website Pros, Inc.
- * as a trade secret, and is not to be used or disclosed except as
- * expressly provided in a written license agreement executed by
- * you and Website Pros, Inc.
- *
- * <copyright@websitepros.com>
- *
- * NOTES
- * JavaScript code.
- *
- *****/
-
- if (!IS.isModuleInitialized("IS.NOF.UTIL.Iterator"))
- {
- /****h* NOF_JavaScript_Library/NOF.UTIL.Iterator
- *
- * NAME
- * NOF.UTIL.Iterator
- *
- * DESCRIPTION
- * An iterator over a collection.
- *
- ****/
- function UTIL_Iterator(/*Collection*/ c, /*boolean*/ safe) {
- this.__proto__ = UTIL_Iterator.prototype;
-
- //TODO: if safe then clone the collection and work on it's copy
-
- this.collection = (safe == true) ? c.toArray() : c; //? c.toArray();
- this.currentIndex = -1;
- this.canDelete = false;
- this.isNextAvailable = this.hasNext();
- }
- {
- var method = UTIL_Iterator.prototype;
- /*
- boolean hasNext()
- Object next()
- void remove()
- */
- /**
- * Returns true if the iteration has more elements.
- **/
- method.hasNext = function () {
- return (this.currentIndex + 1 < this.collection.length);
- }
-
- /**
- * Returns the next element in the iteration.
- **/
- method.next = function () {
-
- if (!this.isNextAvailable) {
- throw new NOF.UTIL.Exception("NoSuchElementException", "NOF.UTIL.Iterator", 67);
- } else {
- this.canDelete = true;
- this.currentIndex++;
- this.isNextAvailable = this.hasNext();
- return this.collection.item(this.currentIndex);
- }
- }
-
- /**
- * Removes from the underlying collection the last element
- * returned by the iterator.
- **/
- method.remove = function () {
- if (!this.canDelete) {
- throw new NOF.UTIL.Exception("IllegalStateException", "NOF.UTIL.Iterator", 82);
- //return;
- }
- if (this.currentIndex < this.collection.length) {
- this.collection.remove(this.currentIndex--);
- this.canDelete = false;
- this.isNextAvailable = this.hasNext();
- }
- }
-
- }
-
- UTIL.__proto__.Iterator = UTIL_Iterator;
- }